﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace TreeInOne_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private bool ComListening = false;//是否没有执行完invoke相关操作              
        private bool ComClosing = false;//是否正在关闭串口，执行Application.DoEvents， 并阻止再次invoke 
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
            RxList = new List<byte>();
            RxList.Clear();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (toolStripButton1.Text == "开始")
            {
                serialPort1.PortName = cbCom.Text;
                ComClosing = false;
                serialPort1.Open();
                toolStripButton1.Text = "停止";
            }
            else
            {
                ComClosing = true;
                while (ComListening)
                    Application.DoEvents();
                serialPort1.Close();
                toolStripButton1.Text = "开始";
            }
        }
        List<byte> RxList;
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte rx;
            if (ComClosing)
                return;
            ComListening = true;
            while (serialPort1.BytesToRead != 0)
            {
                int irx = serialPort1.ReadByte();
                if (irx == -1)
                    break;
                rx = (byte)irx;
                RxList.Add(rx);
                if (RxList.Count == 1)
                {
                    if (RxList[0] != 0xaa)
                        RxList.Clear();
                }
                else if (RxList.Count == 2)
                {
                    if (RxList[1] != 0xaa)
                        RxList.Clear();
                }
                else if (RxList.Count == 10)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        ListViewItem lt = listView1.Items.Add("");
                        lt.SubItems.Add("");
                        lt.SubItems.Add("");
                        lt.SubItems.Add(WFNetLib.Strings.StringsFunction.byteToHexStr(RxList, " "));
                        lt.SubItems.Add(DateTime.Now.ToString());
                        if (WFNetLib.Verify.GetVerify_byteSum(RxList, 2, 7) == RxList[9])
                        {
                            ushort x;
                            float y;
                            x = WFNetLib.BytesOP.MakeShort(RxList[3], RxList[2]);
                            y = (float)x;
                            y = y / 10;
                            y = y - 40;
                            lt.SubItems[0].Text = y.ToString("F1");
                            x = WFNetLib.BytesOP.MakeShort(RxList[5], RxList[4]);
                            y = (float)x;
                            y = y / 10;
                            y = y - 20;
                            lt.SubItems[1].Text = y.ToString("F1");
                            x = WFNetLib.BytesOP.MakeShort(RxList[7], RxList[6]);
                            y = (float)x;
                            y = y / 10;
                            lt.SubItems[2].Text = y.ToString("F1");
                        }                   
                    }));                    
                    RxList.Clear();
                }

            }
            ComListening = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ComClosing = true;
            while (ComListening)
                Application.DoEvents();
            serialPort1.Close();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            while (ComListening)
                Application.DoEvents();
            listView1.Items.Clear();
        }
    }
}
